import 'package:flutter/material.dart';

void main() {
  runApp(ArchitectureDemoApp());
}

/// 主应用
class ArchitectureDemoApp extends StatefulWidget {
  @override
  State<ArchitectureDemoApp> createState() => _ArchitectureDemoAppState();
}

class _ArchitectureDemoAppState extends State<ArchitectureDemoApp> {
  int _selectedIndex = 0;

  final _pages = [
    MvcPage(),
    MvpPage(),
    MvvmPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(['MVC', 'MVP', 'MVVM'][_selectedIndex] + ' 示例'),
          centerTitle: true,
        ),
        body: _pages[_selectedIndex],
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _selectedIndex,
          onTap: (index) => setState(() => _selectedIndex = index),
          items: const [
            BottomNavigationBarItem(icon: Icon(Icons.code), label: 'MVC'),
            BottomNavigationBarItem(icon: Icon(Icons.present_to_all), label: 'MVP'),
            BottomNavigationBarItem(icon: Icon(Icons.view_module), label: 'MVVM'),
          ],
        ),
      ),
    );
  }
}

//
// --------------------------- MVC ---------------------------
//

class CounterModel {
  int count = 0;
  void increment() => count++;
}

class CounterController {
  final CounterModel model;
  CounterController(this.model);
  void increment() => model.increment();
  int get count => model.count;
}

class MvcPage extends StatefulWidget {
  @override
  State<MvcPage> createState() => _MvcPageState();
}

class _MvcPageState extends State<MvcPage> {
  final controller = CounterController(CounterModel());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Count: ${controller.count}', style: TextStyle(fontSize: 32)),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.increment();
          setState(() {});
        },
        child: Icon(Icons.add),
      ),
    );
  }
}

//
// --------------------------- MVP ---------------------------
//

abstract class CounterView {
  void refreshCounter(int count);
}

class CounterPresenter {
  final CounterModel _model = CounterModel();
  late CounterView _view;

  void attachView(CounterView view) {
    _view = view;
    _view.refreshCounter(_model.count);
  }

  void onIncrement() {
    _model.increment();
    _view.refreshCounter(_model.count);
  }
}

class MvpPage extends StatefulWidget {
  @override
  State<MvpPage> createState() => _MvpPageState();
}

class _MvpPageState extends State<MvpPage> implements CounterView {
  final presenter = CounterPresenter();
  int _count = 0;

  @override
  void initState() {
    super.initState();
    presenter.attachView(this);
  }

  @override
  void refreshCounter(int count) {
    setState(() => _count = count);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text('Count: $_count', style: TextStyle(fontSize: 32))),
      floatingActionButton: FloatingActionButton(
        onPressed: presenter.onIncrement,
        child: Icon(Icons.add),
      ),
    );
  }
}

//
// --------------------------- MVVM ---------------------------
//

class CounterViewModel extends ChangeNotifier {
  final CounterModel _model = CounterModel();
  int get count => _model.count;

  void increment() {
    _model.increment();
    notifyListeners();
  }
}

class MvvmPage extends StatelessWidget {
  final viewModel = CounterViewModel();

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: viewModel,
      builder: (context, _) => Scaffold(
        body: Center(
          child: Text('Count: ${viewModel.count}', style: TextStyle(fontSize: 32)),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: viewModel.increment,
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}
